package dao;

import model.Photo;
import util.ConnMySQL;

import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class PhotoDao {
    public int save(Photo q){
        Connection conn = null;
        PreparedStatement preStmt = null;
        ResultSet affectedSet = null;
        String idusers = null;
        try {
            conn = ConnMySQL.getConnection();
            String sql = "insert into photowall(`describe`) values(?)";
            preStmt = conn.prepareStatement(sql);
            preStmt.setString(1, q.getDescribe());
            preStmt.executeUpdate();

            return 1;
        } catch  (SQLDataException ex) {
            if (ex.getErrorCode()==1062){
                return 0;
            }
            ex.printStackTrace();
            return 0;
        }catch (Exception ex){
            ex.printStackTrace();
            return 0;
        }finally {
            if (affectedSet != null) {
                try {
                    affectedSet.close();
                    affectedSet = null;
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

            if (preStmt != null) {
                try {
                    preStmt.close();
                    preStmt = null;
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }

    }








    public List<Photo> findAll() throws Exception {
        Connection conn = null;
        PreparedStatement preStmt = null;
        ResultSet rs = null;
        String sql = "select * from photowall";
        conn=ConnMySQL.getConnection();
        if (conn != null) {
            try {
                preStmt = conn.prepareStatement(sql);
                rs = preStmt.executeQuery();
                List<Photo> qList = rsToQa(rs);
                return qList;
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return null;
    }



    public Photo findById(String idQA) throws Exception {
        Connection conn = null;
        PreparedStatement preStmt = null;
        ResultSet rs = null;
        String sql = "SELECT * FROM qa where idQA=?";
        conn=ConnMySQL.getConnection();
        if (conn != null) {
            try {
                preStmt = conn.prepareStatement(sql);
                preStmt.setString(1, idQA);
                rs = preStmt.executeQuery();
                Photo q = rsToQa(rs).get(0);
                return q;
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return null;
    }


    private List<Photo> rsToQa(ResultSet rs) {
        List<Photo> qList = new ArrayList<>();
        try {
            while (rs.next()) {
                String describe = rs.getString("describe");

                Photo q = new Photo(describe);
                qList.add(q);
            }
            return qList;
        } catch (SQLException e) {
            processSqlError(e);
        }
        return null;
    }

    private void processSqlError(Exception e) {
        e.printStackTrace();
    }

    public static void main(String[] args) {

    }


}
